package com.nowcoder.community.controller;

import com.nowcoder.community.service.AlphaService;
import com.nowcoder.community.utils.CommunityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/alpha")
public class AlphaController {

    @Autowired
    private AlphaService alphaService;

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "Hello Spring Boot";
    }

    @GetMapping("/data")
    @ResponseBody
    public String getData(){
        return alphaService.find();
    }

    // 获取请求参数
    @GetMapping("/students")
    @ResponseBody
    public String getStudents(
            @RequestParam(name = "page", required = false, defaultValue = "1") int page,
            @RequestParam(name = "limit", required = false, defaultValue = "10") int limit){
        return "get students(page: " + page + ", " + limit + ")";
    }

    @GetMapping("/student/{id}")
    @ResponseBody
    public String getStudent(@PathVariable("id") int id){
        return "student id: " + id;
    }

    @PostMapping("/student")
    @ResponseBody
    public String saveStudent(String name, int age){
        return "save student(name: "+ name +", " + "age: "+ age + ")";
    }

    // 渲染模板
    @GetMapping("/teacher")
    public ModelAndView getTeacher(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", "张三");
        mav.addObject("age", 18);
        mav.setViewName("/demo/view");
        return mav;
    }

    @GetMapping("/school")
    public String getSchool(Model model){
        model.addAttribute("name", "北京大学");
        model.addAttribute("age", 80);
        return "/demo/view";
    }

    // 返回 JSON 数据
    @GetMapping("/employee")
    @ResponseBody
    public Map<String, Object> getEmployee(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 23);
        map.put("salary", 1000);
        return map;
    }

    @GetMapping("/employees")
    @ResponseBody
    public List<Map<String, Object>> getEmployees(){
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map;
        map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 23);
        list.add(map);

        map = new HashMap<>();
        map.put("name", "李四");
        map.put("age", 24);
        list.add(map);
        return list;
    }

    // cookie 示例
    // 设置 cookie
    @GetMapping("/cookie/set")
    @ResponseBody
    public String setCookie(HttpServletResponse response){
        Cookie cookie = new Cookie("code", CommunityUtils.generateUUID());
        cookie.setPath("/community/alpha");
        cookie.setMaxAge(60 * 10); // 单位是秒
        response.addCookie(cookie);
        return "set cookie: " + cookie.getValue();
    }

    // 获取 cookie
    @GetMapping("/cookie/get")
    @ResponseBody
    public String getCookie(@CookieValue("code") String code){
        return "get Cookie: " + code;
    }

    // Session 示例
    @GetMapping("/session/set")
    @ResponseBody
    public String setSession(HttpSession session){
        session.setAttribute("code", CommunityUtils.generateUUID());
        return "set session: " + session.getAttribute("code");
    }

    // 获取 session
    @GetMapping("/session/get")
    @ResponseBody
    public String getSession(HttpSession session){
        return "get session: " + session.getAttribute("code");
    }

    // ajax demo
    @PostMapping("/ajax")
    @ResponseBody
    public String ajaxDemo(String name, int age){
        return CommunityUtils.getJsonString(200, "OK");
    }

    @GetMapping("/error")
    public String error(){
        Integer.valueOf("error");
        return "/index";
    }

}
